package com.hzy.orderSystem.utils;

import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class FileUtil {

  public static String uploadFile(MultipartFile file)
      throws IOException {

    // 获取上传的文件名
    String oldFileName = file.getOriginalFilename();
    //    // 使用UUID拼接后缀，定义一个不重复的文件名
    //    String finalName = UUID.randomUUID() + suffix;
    String relativePath = "/files/" + oldFileName;
    String realPath = ResourceUtils.getURL("classpath:").getPath()
           + "/static" + relativePath;
    //String realPath =
    //    "/F:/xiaobao/dazhu/IDEA/pro_manage_sys/target/classes//static/files/" + lastPath;
    File dest = new File(realPath);

    try {
      if (!dest.exists()) dest.mkdirs();
      file.transferTo(dest);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return relativePath;
  }
}
